<?php include("hacs_control/functions/randString.php");
//Set the file to read as import.txt
$file="import.txt";
$fr=fopen($file,'r');
//Read the first line in the file to get the class name
$str=fgets($fr);
$str = trim($str);
echo $str . "<br/>";
//Make sure the string grabbed isn't blank
if(strcmp($str,"")!=0){
	if(!file_exists($str) || !is_dir($str)){
		//Cycle through all the names in the file, create directories etc. for it
		$file2 = "data/Configurations/classlist.txt";
		file_put_contents($file2, $str . "\n", FILE_APPEND);
		$str = "data/Classes/" . $str;
		mkdir($str);
		mkdir($str . "/Assignments");
		$file2=$str . "/Assignments/assignments.txt";
		$fw=fopen($file2,'w');
		fclose($fw);
		copy("hacs_control/functions/index.php",$str . "/index.php");
		mkdir($str . "/Students");
		$file2=$str . "/messages.txt";
		$dir=$str . "/Students/";
		copy("hacs_control/functions/index.php",$dir . "/index.php");
		$fw=fopen($file2,"w");
		fclose($fw);
		$file2=$str . "/passwords.txt";
		//Loop for opening passwords.txt and populating it with students and passwords
		while(!feof($fr)){
			$str=fgets($fr);
			$str=trim($str);
			if(strcmp($str,"") != 0){
				mkdir($dir . $str);
				copy("hacs_control/functions/index.php",$dir . $str . "/index.php");
				$str2=genSalt(7);
				$str .= "," . $str2;
				file_put_contents($file2, $str, FILE_APPEND);
				if(!feof($fr)){
					file_put_contents($file2, "\n", FILE_APPEND);
				}
			}
		}
	}
}
//Close the file and empty it out.
fclose($fr);
$fr=fopen($file,'w');
fclose($fr);
echo "Classes updated successfully";
?>